package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GloryData;
import com.ruoyi.system.domain.PlusGradeItem;
import com.ruoyi.system.mapper.PlusGradeItemMapper;
import com.ruoyi.system.service.IGloryTypeService;
import com.ruoyi.system.service.IPlusGradeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 加分项Service业务层处理
 *
 * @author 王清江
 * @date 2021-01-31
 */
@Service
public class PlusGradeItemServiceImpl implements IPlusGradeItemService {
    /**
     * 镇压警告，能自动装配
     */
    @SuppressWarnings("all")
    @Autowired
    private PlusGradeItemMapper plusGradeItemMapper;

    @Autowired
    private IGloryTypeService gloryTypeService;

    /**
     * 查询加分项
     *
     * @param plusGradeItemId 加分项ID
     * @return 加分项
     */
    @Override
    public PlusGradeItem selectPlusGradeItemById(Long plusGradeItemId) {
        return plusGradeItemMapper.selectPlusGradeItemById(plusGradeItemId);
    }

    /**
     * 查询加分项列表，这里的userAlias是一个妙用，本来应该填u  但是可以填写a  a是plusGradeItem表的别名，这里面也有字段a.user_id，这里可以用索引
     *
     * @param plusGradeItem 加分项
     * @return 加分项
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "a")
    public List<PlusGradeItem> selectPlusGradeItemList(PlusGradeItem plusGradeItem) {
        return plusGradeItemMapper.selectPlusGradeItemList(plusGradeItem);
    }

    /**
     * 新增加分项
     *
     * @param plusGradeItem 加分项
     * @return 结果
     */
    @Override
    public int insertPlusGradeItem(PlusGradeItem plusGradeItem) {
        //添加创建日期和创建者
        plusGradeItem.setCreateTime(DateUtils.getNowDate());
        plusGradeItem.setCreateBy(SecurityUtils.getUsername());
        //获取userId这一步很重要，让userId被填充
        plusGradeItem.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        //两件事  第一，填充荣誉标签（冗余）第二，填充分数（冗余）
        autoLoadGloryLabelAndGrade(plusGradeItem);
        return plusGradeItemMapper.insertPlusGradeItem(plusGradeItem);
    }

    /**
     * 封装方法，从缓存中自动获取荣誉标签和冗余分数填充
     *
     * @param plusGradeItem 需要填充的对象引用
     */
    public void autoLoadGloryLabelAndGrade(PlusGradeItem plusGradeItem) {
        GloryData gloryDataFromCache = gloryTypeService.getGloryDataFromCache(plusGradeItem.getPlusGradeItemGloryId());
        plusGradeItem.setPlusGradeItemGloryLabel(gloryDataFromCache.getGloryLabel());
        plusGradeItem.setGloryGrade(gloryDataFromCache.getGloryGrade());
        //凡是新增、修改任何信息，学生处和学院都是未审核通过的，所以手动赋予状态，都需要重新审核
        plusGradeItem.setStatusCollege("1");
        plusGradeItem.setStatusOffice("1");
    }

    /**
     * 修改加分项
     *
     * @param plusGradeItem 加分项
     * @return 结果
     */
    @Override
    public int updatePlusGradeItem(PlusGradeItem plusGradeItem) {
        plusGradeItem.setUpdateTime(DateUtils.getNowDate());
        plusGradeItem.setUpdateBy(SecurityUtils.getUsername());
        autoLoadGloryLabelAndGrade(plusGradeItem);
        return plusGradeItemMapper.updatePlusGradeItem(plusGradeItem);
    }

    /**
     * updatePlusGradeItem不好用，需要一个能单独更新状态的方法
     *
     * @param plusGradeItem 带状态的对象
     * @return 影响行数
     */
    @Override
    public int updatePlusGradeItemOnlyStatus(PlusGradeItem plusGradeItem) {
        plusGradeItem.setUpdateTime(DateUtils.getNowDate());
        plusGradeItem.setUpdateBy(SecurityUtils.getUsername());
        return plusGradeItemMapper.updatePlusGradeItem(plusGradeItem);
    }

    /**
     * 批量删除加分项
     *
     * @param plusGradeItemIds 需要删除的加分项ID
     * @return 结果
     */
    @Override
    public int deletePlusGradeItemByIds(Long[] plusGradeItemIds) {
        return plusGradeItemMapper.deletePlusGradeItemByIds(plusGradeItemIds);
    }

    /**
     * 删除加分项信息
     *
     * @param plusGradeItemId 加分项ID
     * @return 结果
     */
    @Override
    public int deletePlusGradeItemById(Long plusGradeItemId) {
        return plusGradeItemMapper.deletePlusGradeItemById(plusGradeItemId);
    }

    /**
     * 一款算分机制SQL
     *
     * @param userId 用户Id
     * @return 算分所需列
     */
    @Override
    public List<PlusGradeItem> selectGloryDataByUserId(Long userId) {
        return plusGradeItemMapper.selectGloryDataByUserId(userId);
    }
}
